package com.ms.oschina.base;

import java.io.IOException;
import java.util.List;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.ms.oschina.R;
import com.ms.oschina.adapter.PageIndexListAdapter;
import com.ms.oschina.base.LoadingPager.ResultCode;
import com.ms.oschina.base.LoadingPager.onNetExecuteFinishListener;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.util.UiUtils;

/**
 * 一个含有加载页、成功页、错误页、空白页的ListFragment
 * 
 * @param <T>
 *            网络返回的数据类型
 */
public abstract class BaseListFragment<T> extends BaseFragment {
	private View view;
	private LoadingPager mLp;
	private SwipeRefreshLayout srlRefresh;
	private ListView listView;
	private int pageIndex;
	private PageIndexListAdapter<T> adapter;
	private List<T> list;

	private void findViews(View view) {
		srlRefresh = (SwipeRefreshLayout) view.findViewById(R.id.srl_refresh);
		listView = (ListView) view.findViewById(R.id.lv);
	}

	protected View loadSuccessView() {
		view = UiUtils.inflate(R.layout.pulltorefresh);
		findViews(view);
		setData();
		setEvent();
		return view;
	}

	protected void setEvent() {

	}

	private void setData() {
		setSwipeREfreshLayout();
		adapter = getAdapter();
		listView.setAdapter(adapter);
	}

	private void setSwipeREfreshLayout() {
		srlRefresh.setColorSchemeResources(R.color.swiperefresh_color1,
				R.color.swiperefresh_color2, R.color.swiperefresh_color3,
				R.color.swiperefresh_color4);
		MySwipeRefreshListener listener = new MySwipeRefreshListener();
		srlRefresh.setOnRefreshListener(listener);
	}

	protected ResultCode initNetData() {
		try {
			list = getNetData(pageIndex);
			if (list == null || list.size() == 0) {
				return ResultCode.STATUS_EMPTY;
			}
			return ResultCode.STATUS_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultCode.STATUS_ERRORS;
	}

	public List<T> getNetData() {
		return list;
	}

	public ListView getListView() {
		return listView;
	}

	@Override
	public int getLayoutId() {
		return R.layout.framepager;
	}

	@Override
	public void initView(View view) {
		FrameLayout fl = (FrameLayout) view.findViewById(R.id.fl);
		mLp = new LoadingPager(UiUtils.getContext()) {
			@Override
			public ResultCode initNetData() {
				ResultCode resultCode = BaseListFragment.this.initNetData();
				return resultCode;
			}

			@Override
			public View LoadSuccessView() {
				return BaseListFragment.this.loadSuccessView();
			}
		};
		fl.addView(mLp);
		refreshData();
	}

	public void refreshData() {
		mLp.init();
	}

	public void setErrorPager(final String text, final OnClickListener listener) {
		mLp.setCurStatus(ResultCode.STATUS_EMPTY, true);
		mLp.setErrorMessage(text, listener);
	}

	public class MySwipeRefreshListener implements
			SwipeRefreshLayout.OnRefreshListener {
		@Override
		public void onRefresh() {
			// 重新加载网络
			mLp.getNetData(true, new onNetExecuteFinishListener() {
				@Override
				public void onNetExecuteFinish(ResultCode status) {
					srlRefresh.setRefreshing(false);
					if (status == ResultCode.STATUS_SUCCESS) {
						// 重新设置数据
						adapter.setListData(list);
						adapter.initPageIndex();
					}
				}
			});

		}
	}

	@Override
	protected void initData() {
	}

	/**
	 * 得到页面传递的标记
	 * 
	 * @return
	 */
	protected int getMark() {
		Bundle arguments = getArguments();
		if (arguments != null) {
			return (Integer) arguments.get(Constants.ARGS);
		}
		return 0;
	}

	public abstract List<T> getNetData(int pageIndex) throws IOException;

	public abstract PageIndexListAdapter<T> getAdapter();
}
